package com.gongj.shej.demo;

import com.gongj.shej.策略模式.*;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program: shej
 * @description:
 * @author: gongj
 * @Description: TODO
 * @create: 2020-07-23 19:39
 **/
//工厂  + 单例（饿汉）
public class StrategyFactory{
    private static final Integer A  = 5;
    private static final Integer B  = 10;
    private static final Integer C  = 20;
    private static final Integer D  = 30;

    private static StrategyFactory factory = new StrategyFactory();
    private StrategyFactory(){}

    private static Map<Integer, Strategy> strategyMap = new ConcurrentHashMap<>();
    static {
        strategyMap.put(A,new Strategy5());
        strategyMap.put(B,new Strategy10());
        strategyMap.put(C,new Strategy20());
        strategyMap.put(D,new Strategy30());
    }
    public Strategy creator(int pType){
        if (strategyMap.containsKey(pType)) {
            return strategyMap.get(pType);
        }
        // 根据业务需求，确定默认返回的类型
        return strategyMap.get(A);
    }
    public static StrategyFactory getInstance(){
        return factory;
    }
}